import { changePageStatus, delPageList, getPageList } from '@/service/modules/page';
import { IPageType } from '@/types';
import { Effect, Reducer } from 'umi';

export interface PageModelState {
  pageList: IPageType[],
  pageLength: number
}

export interface PageModelType {
  namespace: 'page';
  state: PageModelState;
  effects: {
    getPageList: Effect;
    delPageList: Effect;
    changePageStatus: Effect;
  };
  reducers: {
    save: Reducer<PageModelState>;
  };
}

const PageModel: PageModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'page',

  // 数据模型状态，相当于vuex的state
  state: {
    pageList: [],
    pageLength: 0
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    // 获取
    *getPageList({ payload }, { call, put }) {
      let res = yield call(getPageList, payload);
      console.log(res)
      if (res.data) {
        yield put({
          type: 'save',
          payload: {
            pageList: res.data[0],
            pageLength: res.data[1]
          }
        })
      }
    },
    // 删除
    *delPageList({ payload }, { call, put }) {
      let res = yield call(delPageList, payload);

    },
    *changePageStatus({payload},{call,put}){
      let res = yield call (changePageStatus,payload);
    }
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default PageModel;
